package com.ld.hpfs.web.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ld.hpfs.common.jackson.JackJson;
import com.ld.hpfs.common.utils.Const;
import com.ld.hpfs.pojo.User;
import com.ld.hpfs.service.PersonalService;
import com.ld.hpfs.web.interseptor.WebConstants;

@Controller
public class PersonalController
{
    private static final Logger logger = LoggerFactory.getLogger(PersonalController.class);
    @Autowired
    private PersonalService      personalService;  //this need 'personalserver' file in java project
	
	 /*
	  * 获取后台图片
	  */
    @RequestMapping(value = "/PersonalImage")
	public void getPersonalImage(HttpSession session, HttpServletRequest request,HttpServletResponse response,PrintWriter writer){
		HashMap<String,Object> param = new HashMap<String,Object>();
    	response.setContentType("text/plain;charset=UTF-8");
		try {
			User user = (User)session.getAttribute(WebConstants.CURRENT_USER);
	        param.put("creator_id", user.getUserId());  //用户ID
//	        String id = user.getUserId();
			String Project = "BUTTON_BAR_RIGHT_BG_IMG";  //项目
			param.put("Project", Project);
			ArrayList<HashMap<String,Object>> result = personalService.getPersonalImage(param);
			String returnMsg = JackJson.fromObjectToJson(result);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
	}
    
     /*
	  * 保存后台图片
	  */
	@RequestMapping(value = "/PersonalImageSave")
    public void SavePersonalImage(@RequestParam String CompanyImg,HttpSession session, HttpServletRequest request,HttpServletResponse response,PrintWriter writer){
		HashMap<String,Object> result = new HashMap<String,Object>();
		User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        if (user == null)
        {
            result.put("resultcode", -99);
        }
    	HashMap<String,Object> param = new HashMap<String,Object>();
    	response.setContentType("text/plain;charset=UTF-8");
    	try {
    		user = (User)session.getAttribute(WebConstants.CURRENT_USER);
    		param.put("creator_id", user.getUserId());  //用户ID
//	    	String id = user.getUserId();
			String Project = "BUTTON_BAR_RIGHT_BG_IMG";  //项目
			param.put("Project", Project);
			param.put("CompanyImg", CompanyImg);//公司图片
			int ret = personalService.SavePersonalImage(param);
			result.put("resultcode", ret);
			writer.print(JackJson.fromObjectToJson(result));
    	}catch (Exception e){
    		logger.error("Exception: ", e);
    	}finally {
			writer.flush();
			writer.close();
		}
    }
	
	/*
	 * 切换System
	 */
	@RequestMapping(value = "/FindSystemAndUser")
	public void FindSystemAndUser(HttpSession session, HttpServletRequest request,HttpServletResponse response,
			PrintWriter writer){
		try{
			HashMap<String,Object> param = new HashMap<String,Object>();
			String System = (String)request.getParameter("System") == "" ? "1" : (String)request.getParameter("System");
			Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
			int CompanyId = Integer.parseInt(currentCompany.toString());
			StringBuilder System_URL = new StringBuilder();
			if(System.equals("1")){
				System_URL.append(Const.PROPERTIES.getProperty("f_url"));
			}else if(System.equals("2")){
				System_URL.append(Const.PROPERTIES.getProperty("t_url"));
			}else if(System.length() < 1){
				System_URL.append(Const.PROPERTIES.getProperty("f_url"));
			}
			param.put("companyId", CompanyId);
			HashMap<String,Object> URL = new HashMap<String,Object>();
			URL.put("System_URL", System_URL);
			ArrayList<HashMap<String,Object>> result = personalService.FindSystemAndUser(param);
			result.add(URL);
			String returnMsg = JackJson.fromObjectToJson(result);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);
		}catch(Exception ex){
			logger.error("Exception: ", ex);
		}finally{
			writer.flush();
			writer.close();
		}
	}
	
	/*
	 * setModifyPassword修改密码
	 * 2013.05.27 BY guxiaoli
	 */
	@RequestMapping(value = "/setModifyPassword")
    public void setModifyPassword(@RequestParam String origin
    		                     ,@RequestParam String newPassword
    		                     ,HttpSession session, HttpServletRequest request,HttpServletResponse response,PrintWriter writer){
		HashMap<String,Object> result = new HashMap<String,Object>();
		HashMap<String,Object> param = new HashMap<String,Object>();
    	response.setContentType("text/plain;charset=UTF-8");
    	try {
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
	        if (user == null)
	        {
	            result.put("resultcode", -99);
	        }else{    		    	
	    		user = (User)session.getAttribute(WebConstants.CURRENT_USER);
	    		param.put("userId", user.getUserId());  //用户ID
				param.put("origin", origin);
				param.put("newPassword", newPassword);//公司图片
				int ret = personalService.setModifyPassword(param);
				result.put("resultcode", ret);
	        }
			writer.print(JackJson.fromObjectToJson(result));
    	}catch (Exception e){
    		logger.error("Exception: ", e);
    	}finally {
			writer.flush();
			writer.close();
		}
    }
}